﻿using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace app.scrum
{

    public class MySqlScrumObject : ScrumObject
    {
        private Hashtable dataTypeMap = new Hashtable();

        public MySqlScrumObject()
        {
            dataTypeMap.Add("DateTime",MySqlDbType.DateTime);
            dataTypeMap.Add("String", MySqlDbType.VarChar);
        }

        public override int create(ScrumObject sObj)
        {
            int num = 0;
            MySqlConnection con = null;
            MySqlCommand cmd = null;
            try
            {
                con = DatabaseManager.getMySqlConnection();
                cmd = con.CreateCommand();
                if (Util.isEmpty(this.createCmdText))
                {
                    this.genCreateCmdText(sObj);
                }
                cmd.CommandText = this.createCmdText;
                foreach (ScrumField sField in sObj.fieldList)
                {
                    MySqlParameter mp = new MySqlParameter("?" + sField.name, MySqlDbType.VarChar);
                    mp.Value = sField.value;
                    cmd.Parameters.Add(mp);
                }
                num = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DatabaseManager.closeResource(con, cmd);
            }
            return num;
        }

        public override int update(ScrumObject sObj)
        {
            int num = 0;
            DbConnection con = null;
            DbCommand cmd = null;
            try
            {
                con = DatabaseManager.getDbConnection();
                cmd = con.CreateCommand();
                if (Util.isEmpty(this.updateCmdText))
                {
                    this.genUpdateCmdText(sObj);
                }
                cmd.CommandText = this.updateCmdText;
                if (cmd.Parameters.Count < 0)
                {
                    foreach (ScrumField sField in sObj.fieldList)
                    {
                        if (String.Equals(sField.name, "id__c")
                            || String.Equals(sField.name, "objecttype"))
                        {
                            continue;
                        }
                        else
                        {
                            if (sField.value != null)
                            {
                                MySqlParameter mp = new MySqlParameter("?" + sField.name, dataTypeMap[sField.typeName]);
                                mp.Value = sField.value;
                                cmd.Parameters.Add(mp);
                            }
                        }
                    }
                }
                num = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DatabaseManager.closeResource(con, cmd);
            }
            return num;
        }

        public override void genCreateCmdText(ScrumObject sObj)
        {
            this.createCmdText = "insert into " + this.tableName + " ( ";
            foreach (ScrumField sField in sObj.fieldList)
            {
                if (String.Equals(sField.name, "objecttype"))
                {
                    continue;
                }
                this.createCmdText += "" + sField.name + ",";
            }
            this.createCmdText = this.createCmdText.Substring(0, this.createCmdText.Length - 1);
            this.createCmdText += ") values (";
            foreach (ScrumField sField in sObj.fieldList)
            {
                if (String.Equals(sField.name, "objecttype"))
                {
                    continue;
                }
                this.createCmdText += "?" + sField.name + ",";
            }
            this.createCmdText = this.createCmdText.Substring(0, this.createCmdText.Length - 1);
            this.createCmdText += ")";
        }

        public override void genUpdateCmdText(ScrumObject sObj)
        {
            this.updateCmdText = "update " + this.tableName + " set ";
            foreach (ScrumField sField in sObj.fieldList)
            {
                if (String.Equals(sField.name, "objecttype")
                    || String.Equals(sField.name, "id__c"))
                {
                    continue;
                }
                this.updateCmdText += sField.name + "=@" + sField.name + ",";

            }
            this.updateCmdText = this.updateCmdText.Substring(0, this.updateCmdText.Length - 1);
            //this.updateCmdText += " where " + ScrumField.fieldName_Id + "=" + ScrumObjectHelper.getScrumFieldValue(sObj, ScrumField.fieldName_Id);
        }
    }
}